package com.huashi.dealer.modular.business.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huashi.dealer.commonEntity.Article;
import com.huashi.dealer.commonEntity.ArticleCategory;
import com.huashi.dealer.commonEntity.UploadFile;
import com.huashi.dealer.core.exception.CommonException;
import com.huashi.dealer.core.exception.ServiceException;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.util.BizAssertUtil;
import com.huashi.dealer.modular.business.article.mapper.ArticleMapper;
import com.huashi.dealer.modular.business.article.service.ArticleCategoryService;
import com.huashi.dealer.modular.business.article.service.ArticleService;
import com.huashi.dealer.modular.business.article.param.ArticleResponseParam;
import com.huashi.dealer.modular.business.article.vo.ArticleVo;
import com.huashi.dealer.modular.business.upload.service.UploadService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * API文章Service实现类
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Service("apiArticleServiceImpl")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    private ArticleCategoryService articleCategoryService;
    @Resource
    private UploadService uploadService;
    @Override
    public Page<Article> getArticleList(Long categoryId) {
        Page<Article> page= PageFactory.defaultPage();
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(categoryId),Article::getCategoryId, categoryId);
        return this.page(page,queryWrapper);
    }

    @Override
    public ArticleResponseParam getArticleListWithoutParams() {
        ArticleResponseParam responseParam = new ArticleResponseParam();

        try {
            // TODO: 获取文章列表（无参数）
            // 这里需要注入ArticleMapper并查询数据库

            // 模拟文章列表
            List<ArticleResponseParam.CategoryInfo> categoryList = new ArrayList<>();
            ArticleResponseParam.CategoryInfo category = new ArticleResponseParam.CategoryInfo();
            category.setCategoryId(1L);
            category.setName("默认分类");
            category.setSort(1);
            category.setWxappId(10001L);
            category.setCreateTime(LocalDateTime.now());

            // 设置分类下的文章列表
            List<ArticleResponseParam.ArticleInfo> articleList = new ArrayList<>();
            ArticleResponseParam.ArticleInfo article = new ArticleResponseParam.ArticleInfo();
            article.setArticleId(1L);
            article.setArticleTitle("默认文章");
            article.setArticleContent("默认文章内容");
            article.setShowType(10);
            article.setCategoryId(1L);
            article.setImageId(1L);
            article.setArticleSort(1);
            article.setArticleStatus(10);
            article.setWxappId(10001L);
            article.setCreateTime(LocalDateTime.now());
            articleList.add(article);
            category.setList(articleList);

            categoryList.add(category);
            responseParam.setCategoryList(categoryList);

        } catch (Exception e) {
            throw new CommonException("获取文章列表失败：" + e.getMessage());
        }

        return responseParam;
    }

    @Override
    public List<ArticleCategory> getCategoryList() {
        return articleCategoryService.list();
    }

    @Override
    public ArticleVo getArticleDetail(Long articleId) {
        ArticleVo responseParam = new ArticleVo();
        BizAssertUtil.notNull(articleId, 1001, "文章ID不能为空");
        Article article = this.getById(articleId);
        Optional.ofNullable(article).orElseThrow(() -> new ServiceException("文章不存在"));
        BeanUtil.copyProperties(article, responseParam);
        ArticleCategory articleCategory = articleCategoryService.getById(article.getCategoryId());
        responseParam.setCategory(articleCategory);
        UploadFile file = uploadService.getById(article.getImageId());
        responseParam.setImage(file);
        responseParam.setShowViews(article.getActualViews() + article.getVirtualViews());
        return responseParam;
    }

    @Override
    public ArticleResponseParam getHotArticles(String token, Integer limit) {
        ArticleResponseParam responseParam = new ArticleResponseParam();

        try {
            // TODO: 获取热门文章
            // 这里需要注入ArticleMapper并查询数据库

            // 模拟热门文章
            List<ArticleResponseParam.ArticleInfo> articleList = new ArrayList<>();
            ArticleResponseParam.ArticleInfo article = new ArticleResponseParam.ArticleInfo();
            article.setArticleId(1L);
            article.setArticleTitle("热门文章标题");
            article.setArticleContent("热门文章内容");
            article.setShowType(10);
            article.setCategoryId(1L);
            article.setImageId(1L);
            article.setArticleSort(1);
            article.setArticleStatus(10);
            article.setWxappId(10001L);
            article.setCreateTime(LocalDateTime.now());
            articleList.add(article);

            responseParam.setArticleList(articleList);

        } catch (Exception e) {
            throw new CommonException("获取热门文章失败：" + e.getMessage());
        }

        return responseParam;
    }

    @Override
    public ArticleResponseParam getRecommendArticles(String token, Integer limit) {
        ArticleResponseParam responseParam = new ArticleResponseParam();

        try {
            // TODO: 获取推荐文章
            // 这里需要注入ArticleMapper并查询数据库

            // 模拟推荐文章
            List<ArticleResponseParam.ArticleInfo> articleList = new ArrayList<>();
            ArticleResponseParam.ArticleInfo article = new ArticleResponseParam.ArticleInfo();
            article.setArticleId(1L);
            article.setArticleTitle("推荐文章标题");
            article.setArticleContent("推荐文章内容");
            article.setShowType(10);
            article.setCategoryId(1L);
            article.setImageId(1L);
            article.setArticleSort(1);
            article.setArticleStatus(10);
            article.setWxappId(10001L);
            article.setCreateTime(LocalDateTime.now());
            articleList.add(article);

            responseParam.setArticleList(articleList);

        } catch (Exception e) {
            throw new CommonException("获取推荐文章失败：" + e.getMessage());
        }

        return responseParam;
    }

    @Override
    public ArticleResponseParam searchArticles(String keyword, String token, Long categoryId, Integer page, Integer listRows) {
        ArticleResponseParam responseParam = new ArticleResponseParam();

        try {
            if (keyword == null || keyword.isEmpty()) {
                throw new CommonException("搜索关键词不能为空");
            }

            // TODO: 搜索文章
            // 这里需要注入ArticleMapper并查询数据库

            // 模拟搜索结果
            List<ArticleResponseParam.ArticleInfo> articleList = new ArrayList<>();
            ArticleResponseParam.ArticleInfo article = new ArticleResponseParam.ArticleInfo();
            article.setArticleId(1L);
            article.setArticleTitle("搜索结果：" + keyword);
            article.setArticleContent("搜索结果内容");
            article.setShowType(10);
            article.setCategoryId(categoryId != null ? categoryId : 1L);
            article.setImageId(1L);
            article.setArticleSort(1);
            article.setArticleStatus(10);
            article.setWxappId(10001L);
            article.setCreateTime(LocalDateTime.now());
            articleList.add(article);

            responseParam.setArticleList(articleList);
            responseParam.setTotal(1L);
            responseParam.setCurrentPage(page != null ? page : 1);
            responseParam.setPerPage(listRows != null ? listRows : 10);
            responseParam.setLastPage(1);

        } catch (Exception e) {
            throw new CommonException("搜索文章失败：" + e.getMessage());
        }

        return responseParam;
    }
} 